package com.huawei.secure.android.common.ssl;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import com.huawei.secure.android.common.external.SecurityCertificateManager;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Arrays;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.X509TrustManager;
import org.apache.http.conn.ssl.BrowserCompatHostnameVerifier;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.conn.ssl.StrictHostnameVerifier;
import org.apache.http.conn.ssl.X509HostnameVerifier;

public class SecureApacheSSLSocketFactory extends SSLSocketFactory {
    public static final X509HostnameVerifier BROWSER_COMPATIBLE_HOSTNAME_VERIFIER = new BrowserCompatHostnameVerifier();
    private static volatile SecureApacheSSLSocketFactory M = null;
    public static final X509HostnameVerifier STRICT_HOSTNAME_VERIFIER = new StrictHostnameVerifier();
    private static final String TAG = "SecureApacheSF";
    private SSLSocket A;
    private String[] B;
    private X509TrustManager C;
    private SSLContext g;
    private Context p;

    private SecureApacheSSLSocketFactory(KeyStore keyStore) throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException {
        super(keyStore);
        this.A = null;
    }

    private SecureApacheSSLSocketFactory(KeyStore keyStore, Context context) throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException, CertificateException, IOException, IllegalArgumentException {
        super(keyStore);
        this.A = null;
        if (context == null) {
            Log.e(TAG, "SecureSSLSocketFactory: context is null");
            return;
        }
        setContext(context);
        setSslContext(SSLUtil.setSSLContext());
        this.C = d(SecurityCertificateManager.getInstance(context).getBksPath());
        this.g.init(null, new X509TrustManager[]{this.C}, new SecureRandom());
    }

    public SecureApacheSSLSocketFactory(KeyStore keyStore, InputStream inputStream, String str) throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException, CertificateException, IOException, IllegalArgumentException {
        super(keyStore);
        this.A = null;
        this.g = SSLUtil.setSSLContext();
        HiCloudX509TrustManager hiCloudX509TrustManager = new HiCloudX509TrustManager(inputStream, str);
        this.g.init(null, new X509TrustManager[]{hiCloudX509TrustManager}, new SecureRandom());
    }

    public SecureApacheSSLSocketFactory(KeyStore keyStore, X509TrustManager x509TrustManager) throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException, CertificateException, IOException, IllegalArgumentException {
        super(keyStore);
        this.A = null;
        this.g = SSLUtil.setSSLContext();
        this.g.init(null, new X509TrustManager[]{x509TrustManager}, new SecureRandom());
    }

    public static SecureApacheSSLSocketFactory getInstance(KeyStore keyStore, Context context) throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException, CertificateException, IOException, IllegalArgumentException {
        if (M == null) {
            synchronized (SecureApacheSSLSocketFactory.class) {
                if (M == null) {
                    M = new SecureApacheSSLSocketFactory(keyStore, context);
                }
            }
        }
        return M;
    }

    public void updateX509TrustManager(String str) throws KeyManagementException, CertificateException, NoSuchAlgorithmException, KeyStoreException, IOException {
        Log.i(TAG, "updateX509TrustManager: ");
        this.C = d(str);
        if (this.g != null) {
            this.g.init(null, new X509TrustManager[]{this.C}, new SecureRandom());
        }
    }

    private X509TrustManager d(String str) throws CertificateException, NoSuchAlgorithmException, KeyStoreException, IOException {
        Log.i(TAG, "getLastX509TrustManger: ");
        if (!TextUtils.isEmpty(str)) {
            return new SecureX509TrustManager(str, false);
        }
        Log.d(TAG, "getLastX509TrustManger: bks path is null");
        return new SecureX509TrustManager(this.p);
    }

    public SSLContext getSslContext() {
        return this.g;
    }

    public void setSslContext(SSLContext sSLContext) {
        this.g = sSLContext;
    }

    public Context getContext() {
        return this.p;
    }

    public void setContext(Context context) {
        this.p = context.getApplicationContext();
    }

    public String[] getSupportedCipherSuites() {
        if (this.B != null) {
            return this.B;
        }
        return new String[0];
    }

    public SSLSocket getSslSocket() {
        return this.A;
    }

    public void setSslSocket(SSLSocket sSLSocket) {
        this.A = sSLSocket;
    }

    public Socket createSocket(Socket socket, String str, int i, boolean z) throws IOException {
        Log.i(TAG, "createSocket: socket host port autoClose");
        Socket createSocket = this.g.getSocketFactory().createSocket(socket, str, i, z);
        if (createSocket instanceof SSLSocket) {
            if (this.p == null || !Arrays.asList(SecureSSLSocketFactory.O).contains(this.p.getPackageName())) {
                SSLUtil.setSSLSocketOptions((SSLSocket) createSocket);
            } else {
                SSLUtil.setSSLSocketOptionsForVmall((SSLSocket) createSocket);
            }
            this.A = (SSLSocket) createSocket;
            this.B = (String[]) this.A.getEnabledCipherSuites().clone();
        }
        return createSocket;
    }

    public Socket createSocket() throws IOException {
        Log.i(TAG, "createSocket: ");
        Socket createSocket = this.g.getSocketFactory().createSocket();
        if (createSocket instanceof SSLSocket) {
            if (this.p == null || !Arrays.asList(SecureSSLSocketFactory.O).contains(this.p.getPackageName())) {
                SSLUtil.setSSLSocketOptions((SSLSocket) createSocket);
            } else {
                SSLUtil.setSSLSocketOptionsForVmall((SSLSocket) createSocket);
            }
            this.A = (SSLSocket) createSocket;
            this.B = (String[]) this.A.getEnabledCipherSuites().clone();
        }
        return createSocket;
    }

    public X509Certificate[] getChain() {
        if (this.C instanceof SecureX509TrustManager) {
            return ((SecureX509TrustManager) this.C).getChain();
        }
        return new X509Certificate[0];
    }
}
